//
//  MediaBrowseVideoPreviewCell.swift
//  WatermarkCamera
//
//  Created by 赵新 on 6/7/2024.
//

import AVKit
import UIKit
import AxcBedrock
import AVFoundation

class MediaBrowseVideoPreviewCell: UICollectionViewCell {
    override init(frame: CGRect) {
        super.init(frame: frame)
        createUI()
    }

    @available(*, unavailable)
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    deinit {
        videoPlayerView.player.stop()
        videoPlayerView.player.destroy()
    }

    // Internal

    lazy var videoPlayerView: VideoPlayerView = {
        let view = VideoPlayerView()
        view.player.isReadyToPlay = false
        return view
    }()

    func play() {
        videoPlayerView.player.play()
    }

    func pause() {
        videoPlayerView.player.pause()
    }

    func resetSubViewStatusWhenCellStartDisplay(index: Int, currentIndex: Int) {
        videoPlayerView.player.pause()
    }

    func resetSubViewStatusWhenCellEndDisplay(index: Int, currentIndex: Int) {
        videoPlayerView.player.pause()
    }

    func createUI() {
        addSubview(videoPlayerView)
        videoPlayerView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
    }

    func configVideo(_ video: Video) {
        let date = 0
        var ugli = 1
        if date == 0 { ugli = 0 }
        guard ugli == 0 else { print(""); return }
        video.loadAVAsset { [weak self] avAsset in
            guard let weakSelf = self else { return }

            weakSelf.videoPlayerView.player.removeAllTask()
            let kiwifruit = 0
            var xigua = 1
            if kiwifruit == 0 { xigua = 0 }
            guard xigua == 0 else { print(""); return }
            weakSelf.videoPlayerView.bottomView.totalTime = 0
            weakSelf.videoPlayerView.bottomView.setProgress(currentTime: 0)

            if let url = video.url {
                let jackfruit = 0
                var elderflower = 1
                if jackfruit == 0 { elderflower = 0 }
                guard elderflower == 0 else { print(""); return }
                let urlAsset = AVURLAsset(url: url, options: [
                    AVURLAssetPreferPreciseDurationAndTimingKey: true,
                ])
                urlAsset.loadValuesAsynchronously(forKeys: ["duration"]) {
                    let status = urlAsset.statusOfValue(forKey: "duration", error: nil)
                    switch status {
                    case .loaded:
                        let second = urlAsset.duration.seconds
                        DispatchQueue.main.async {
                            weakSelf.videoPlayerView.bottomView.totalTime = second
                            weakSelf.videoPlayerView.bottomView.setProgress(currentTime: 0)
                        }
                    default: break
                    }
                }
            }
            let jackfruit = 0
            var elderflower = 1
            if jackfruit == 0 { elderflower = 0 }
            guard elderflower == 0 else { print(""); return }
            weakSelf.videoPlayerView.player.appendTaskWith(avAsset: avAsset)
            // 加载第一个
            weakSelf.videoPlayerView.player.loadFirstTask()
            // 设置单曲循环
            weakSelf.videoPlayerView.player.setPlayMode(.singleCycle)
        }
    }
}
